// 牌的处理
var MaJiang = require('MaJiang');
import Defines from './../MJ/defines'

cc.Class({
    extends: cc.Component,

    properties: {
        m_cardsDianJi : cc.Node,                // 牌不可点击
        m_gangPengHu : cc.Node,                 // 杠碰胡过按钮的父节点
        m_gangPeng : [cc.Node],                 // 自己的杠碰的牌显示位置的父节点
        m_ShowPai : cc.SpriteAtlas,             // 自己的手牌图集
        m_ShowPai02 : cc.SpriteAtlas,           // 堆牌自己和自己对面的图集
        m_ShowPai13 : cc.SpriteAtlas,           // 左右的的图集
        m_ShoPai : cc.Node,                     // 手牌的父节点
        m_tanPaiNode : cc.Node,                 // 摊牌的父节点
        m_PlayerNode0 : [cc.Node],              // 自己的背面牌堆
        m_PlayerNode1 : [cc.Node],              // 左手背面牌堆
        m_PlayerNode2 : [cc.Node],              // 对面背面牌堆
        m_PlayerNode3 : [cc.Node],              // 右手背面牌堆
        m_playerTanPai0 : [cc.Node],            // 自己的摊牌（亮牌）胡牌显示
        m_playerTanPai1 : [cc.Node],            // 左手玩家的摊牌（亮牌）胡牌显示
        m_playerTanPai2 : [cc.Node],            // 对面玩家的摊牌（亮牌）胡牌显示
        m_playerTanPai3 : [cc.Node],            // 右手玩家的摊牌（亮牌）胡牌显示
        m_chat : cc.Node,                       // 聊天的父节点
        m_myMJArrSprite:[],                     // 我的手牌图片
        m_MJArrNode1:[],                        // 左玩家手上的牌
        m_MJArrNode2:[],                        // 上玩家手上的牌
        m_MJArrNode3:[],                        // 右玩家手上的牌
        m_myMJArr:[],                           // 我的手牌
        m_selectedMJ:null,                      // 点击牌的处理
        m_paiDuiArr0:[],                        // 自己显示牌堆
        m_paiDuiArr1:[],                        // 显示牌堆
        m_paiDuiArr2:[],                        // 显示牌堆
        m_paiDuiArr3:[],                        // 显示牌堆
        m_gangChengJi1:0,                       // 右手变玩家碰杠的层级
        m_playerChatNode:[],                    // 玩家聊天的节点
    },

    // onLoad () {},

    start () {
        this.initMJ(this.data);

        // ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓临时↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
        this.ndsadhsakdnask = 0; ///////////////////******************************////////////// */
        this.ndsadhsakdnask111 = 0;
        this.ndsadhsak = 0;



        // ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
    },

    initMJ : function(data){
        //搜索需要的子节点
        var bgChild = this.node.getChildByName("bg");
        //牌的节点
        var paiChild = bgChild.getChildByName("pai");

        //****************************************************| 手牌保存 |---------------------------------------------
        //正常玩游戏的牌
        var shouPaiChild = paiChild.getChildByName("shoupai");
        //自己的牌
        var player0Child = shouPaiChild.getChildByName("player0");
        var player1Child = shouPaiChild.getChildByName("player1");
        var player2Child = shouPaiChild.getChildByName("player2");
        var player3Child = shouPaiChild.getChildByName("player3");

        for(var i = player0Child.children.length-1; i >= 0 ; --i) {
            var sprite = player0Child.children[i].getChildByName("card").getComponent(cc.Sprite);
            var node = player0Child.children[i];
            
            this.m_myMJArrSprite.push(sprite);
            this.m_myMJArr.push(node);
        }

         for(var i = player1Child.children.length-1; i >= 0 ; --i) {
            var node = player1Child.children[13-i];
            this.m_MJArrNode1.push(node);

            var node = player2Child.children[i];
            this.m_MJArrNode2.push(node);

            var node = player3Child.children[i];
            this.m_MJArrNode3.push(node);
        }

        //****************************************************| 显示牌堆 |---------------------------------------------
        var paiDuiChild = paiChild.getChildByName("zhuomianpai");
        var player0 = paiDuiChild.getChildByName("player0");
        var player1 = paiDuiChild.getChildByName("player1");
        var player2 = paiDuiChild.getChildByName("player2");
        var player3 = paiDuiChild.getChildByName("player3");

        for (var paiDuiNum = 0; paiDuiNum < player0.children.length; paiDuiNum++) {
            var node0 = player0.children[paiDuiNum];
            var node1 = player1.children[paiDuiNum];
            var node2 = player2.children[paiDuiNum];
            var node3 = player3.children[paiDuiNum];

            node0.active = false;
            node1.active = false;
            node2.active = false;
            node3.active = false;
            
            var paiDui0 = player0.getChildByName("pai"+(paiDuiNum+1));
            this.m_paiDuiArr0.push(paiDui0);
            var paiDui1 = player1.getChildByName("pai"+(paiDuiNum+1));
            this.m_paiDuiArr1.push(paiDui1);
            var paiDui2 = player2.getChildByName("pai"+(paiDuiNum+1));
            this.m_paiDuiArr2.push(paiDui2);
            var paiDui3 = player3.getChildByName("pai"+(paiDuiNum+1));
            this.m_paiDuiArr3.push(paiDui3);
        }
       
        paiDuiChild.active = true;

        //****************************************************| 聊天 |---------------------------------------------
        for (var num = 0; num < this.m_chat.children.length; num++) {
            this.m_playerChatNode.push(this.m_chat.children[num]);
        }
        
    },

    // 发牌后显示的处理(发牌将发的牌添加到最后面的处理)
    SetShowPlayerCards : function() {
        // 手上的牌设置牌值
        for (var num = Defines.playerData.info.length-1; num >= 0; --num) {
            var card = this.m_myMJArr[num].getChildByName("card").getComponent(cc.Sprite);
            this.m_myMJArr[num].active = true;

            if(num == 0) {// 将新加的牌放到最后1张
                var frame = this.m_ShowPai.getSpriteFrame(MaJiang.cardPng(Defines.playerData.info[Defines.playerData.info.length-1])); 
                // 牌值绑定到牌上
                card.paiData = Defines.playerData.info[Defines.playerData.info.length-1];
            } else {// 将前面的牌放到前面
                var frame = this.m_ShowPai.getSpriteFrame(MaJiang.cardPng(Defines.playerData.info[num-1])); 
                // 牌值绑定到牌上
                card.paiData = Defines.playerData.info[num-1];
            }
            card.spriteFrame = frame;
        }
    },

    // 排序删除点击的牌(出牌后删除发出去的牌的处理)
    SetListPlayerCards : function(mjId) {
        for (var num = 0; num < Defines.playerData.info.length; num++) {

            if(Defines.playerData.info[num] == mjId) {
                Defines.playerData.info.splice(num, 1);
                break;
            }
        }
        MaJiang.cardList(Defines.playerData.info);

        for (var num = 0; num < 14; num++) {
            var card = this.m_myMJArr[num].getChildByName("card").getComponent(cc.Sprite);
            this.m_myMJArr[num].active = false;
        }

        // 手上的牌设置牌值
        for (var num = 1; num < Defines.playerData.info.length+1; num++) {
            if(num >= 14) { break; }

            var card = this.m_myMJArr[num].getChildByName("card").getComponent(cc.Sprite);
            this.m_myMJArr[num].active = true;
            var frame = this.m_ShowPai.getSpriteFrame(MaJiang.cardPng(Defines.playerData.info[num-1]));                                                         /////////////////////////////////改
            card.spriteFrame = frame;
            // 牌值绑定到牌上
            card.paiData = Defines.playerData.info[num-1];
        }
    },

    // 碰杠后的处理
    SetPengGangPlayerCards : function() {
        for (var num = 0; num < 14; num++) {
            var card = this.m_myMJArr[num].getChildByName("card").getComponent(cc.Sprite);
            this.m_myMJArr[num].active = false;
        }

        // 手上的牌设置牌值
        for (var num = 0; num < Defines.playerData.info.length; num++) {
            if(num >= 14) { break; }

            var card = this.m_myMJArr[num].getChildByName("card").getComponent(cc.Sprite);
            this.m_myMJArr[num].active = true;
            var frame = this.m_ShowPai.getSpriteFrame(MaJiang.cardPng(Defines.playerData.info[num]));                                                         /////////////////////////////////改
            card.spriteFrame = frame;
            // 牌值绑定到牌上
            card.paiData = Defines.playerData.info[num];
        }
    },

    // 显示碰杠胡过按钮
    // btnName                  按钮的名字
    // paiNum                   碰牌的牌值
    addOption : function(btnName,paiNum) {
        for(var i = 0; i < this.m_gangPengHu.childrenCount; ++i){
            var child = this.m_gangPengHu.children[i]; 

            if(child.name == "op" && child.active == false){
                child.active = true;
                var pai = child.getChildByName("pai");
                var sprite = pai.getChildByName("card").getComponent(cc.Sprite);
                sprite.spriteFrame = this.m_ShowPai.getSpriteFrame(MaJiang.cardPng(paiNum));                                 /////////////////////////////////改
                var btn = child.getChildByName(btnName); 
                btn.active = true;
                btn.pai = paiNum;
                return;
            }
        }
    },

    // 隐藏碰杠胡过按钮
    hideOptions : function() {
        this.m_gangPengHu.active = false;
        for(var i = 0; i < this.m_gangPengHu.childrenCount; ++i){
            var child = this.m_gangPengHu.children[i]; 

            if(child.name == "op"){
                child.active = false;
                child.getChildByName("peng").active = false;
                child.getChildByName("gang").active = false;
                child.getChildByName("hu").active = false;
            }
        }
    },
    
    // this.data = cc.Node;
    // this.data.hu = true;
    // this.data.gang = false;
    // this.data.peng = true;
    // this.data.pai = 0x01;
    // this.showAction(this.data);
    // 碰杠胡过的回调
    showAction : function(data) {
        if(this.m_gangPengHu.active) {
            this.hideOptions();
        }
        
        if(data && (data.hu || data.gang || data.peng)) {
            this.m_gangPengHu.active = true;
            if(data.hu) {
                this.addOption("hu",data.pai);
            }

            if(data.peng) {
                this.addOption("peng",data.pai);
            }

            if(data.gang) {
                this.addOption("gang",data.pai);
                // for(var i = 0; i < data.gangpai.length;++i){
                //     var gp = data.gangpai[i];
                //     this.addOption("gang",gp);
                // }
            }
        }
    },

    // ***************************************************************************************出牌的牌堆
    // 自己和其他玩家的牌堆显示设置
    // dataArr                          堆牌的数据数组
    // paiDui                           牌堆
    // showPai                          牌的图集
    SetPaiDui : function(dataArr, paiDui, showPai) {
        if(dataArr.length > 24) { return; }

        for (var num = 0; num < paiDui.length; num++) {
            paiDui[num].active = false;
        }

        for (var num = 0; num < dataArr.length; num++) {
            var spri = paiDui[num].getChildByName("card").getComponent(cc.Sprite);
            var frame = showPai.getSpriteFrame(MaJiang.cardPng(dataArr[num]));
            spri.spriteFrame = frame;
            paiDui[num].active = true;
        }
    },

    // 牌堆设置
    // PlayerID                         玩家ID
    // dataArr                          堆牌的数据数组
    SetPlayerPaiDui : function(playerID, dataArr) {
        if(playerID == 0) {
            this.SetPaiDui(dataArr, this.m_paiDuiArr0, this.m_ShowPai02);
        } else if(playerID == 1) {
            this.SetPaiDui(dataArr, this.m_paiDuiArr1, this.m_ShowPai13);
        } else if(playerID == 2) {
            this.SetPaiDui(dataArr, this.m_paiDuiArr2, this.m_ShowPai02);
        } else if(playerID == 3) {
            this.SetPaiDui(dataArr, this.m_paiDuiArr3, this.m_ShowPai13);
        }
    },
    
    // ***************************************************************************************暗牌的牌堆
    // 暗牌牌堆拿掉的牌消失（单张拿牌）
    SetDisappearCard : function() {
        // 牌堆的牌消失
        if(0 == Defines.playerData.PaiDuiPlayrID) {
            this.m_PlayerNode0[Defines.playerData.PaiDuiCardNum].active = false;
        } else if(1 == Defines.playerData.PaiDuiPlayrID) {
            this.m_PlayerNode1[Defines.playerData.PaiDuiCardNum].active = false;
        } else if(2 == Defines.playerData.PaiDuiPlayrID) {
            this.m_PlayerNode2[Defines.playerData.PaiDuiCardNum].active = false;
        } else if(3 == Defines.playerData.PaiDuiPlayrID) {
            this.m_PlayerNode3[Defines.playerData.PaiDuiCardNum].active = false;
        }

        // 牌堆的牌消失后如果牌堆的牌拿完了就结束游戏
        if(Defines.playerData.KaiShiTakeCardID == Defines.playerData.PaiDuiCardNum && Defines.playerData.PaiDuiPlayrID ==  Defines.playerData.KaiShiTakePlayrID) {
            return false;
        }
        
        // 没拿完自动后加
        Defines.playerData.PaiDuiCardNum+=1;
        if(Defines.playerData.PaiDuiCardNum == 34) {
            Defines.playerData.PaiDuiPlayrID += 1;

            // 开始牌堆的ID
            if(4 <= Defines.playerData.PaiDuiPlayrID) {
                Defines.playerData.PaiDuiPlayrID = 0;
            }
            Defines.playerData.PaiDuiCardNum = 0;
        }

        return true;
    },

    // 杠牌后暗牌牌堆拿掉最后一张牌的处理
    SetGangPaiDisappearCard : function() {
        // 牌堆的牌消失
        if(0 == Defines.playerData.KaiShiTakePlayrID) {
            if(Defines.playerData.KaiShiTakeCardID > 0 && this.m_PlayerNode0[Defines.playerData.KaiShiTakeCardID-1].active) {
                this.m_PlayerNode0[Defines.playerData.KaiShiTakeCardID-1].active = false;
                Defines.playerData.KaiShiTakeCardID += 1;
            } else {
                this.m_PlayerNode0[Defines.playerData.KaiShiTakeCardID].active = false;
                Defines.playerData.KaiShiTakeCardID -= 1;
            }
        } else if(1 == Defines.playerData.KaiShiTakePlayrID) {
            if(Defines.playerData.KaiShiTakeCardID > 0 && this.m_PlayerNode1[Defines.playerData.KaiShiTakeCardID-1].active)
            {
                this.m_PlayerNode1[Defines.playerData.KaiShiTakeCardID-1].active = false;
                Defines.playerData.KaiShiTakeCardID += 1;
            } else {
                this.m_PlayerNode1[Defines.playerData.KaiShiTakeCardID].active = false;
                Defines.playerData.KaiShiTakeCardID -= 1;
            }
        } else if(2 == Defines.playerData.KaiShiTakePlayrID) {
            if(Defines.playerData.KaiShiTakeCardID > 0 && this.m_PlayerNode2[Defines.playerData.KaiShiTakeCardID-1].active) {
                this.m_PlayerNode2[Defines.playerData.KaiShiTakeCardID-1].active = false;
                Defines.playerData.KaiShiTakeCardID += 1;
            } else {
                this.m_PlayerNode2[Defines.playerData.KaiShiTakeCardID].active = false;
                Defines.playerData.KaiShiTakeCardID -= 1;
            }
        } else if(3 == Defines.playerData.KaiShiTakePlayrID) {
            if(Defines.playerData.KaiShiTakeCardID > 0 && this.m_PlayerNode3[Defines.playerData.KaiShiTakeCardID-1].active) {
                this.m_PlayerNode3[Defines.playerData.KaiShiTakeCardID-1].active = false;
                Defines.playerData.KaiShiTakeCardID += 1;
            } else {
                this.m_PlayerNode3[Defines.playerData.KaiShiTakeCardID].active = false;
                Defines.playerData.KaiShiTakeCardID -= 1;
            }
        }

        Defines.playerData.KaiShiTakeCardID -= 1;

        if(Defines.playerData.KaiShiTakeCardID < 0) {
            Defines.playerData.KaiShiTakeCardID = 33;
            Defines.playerData.KaiShiTakePlayrID -= 1;

            if(Defines.playerData.KaiShiTakePlayrID < 0) {
                Defines.playerData.KaiShiTakePlayrID = 3;
            }
        }
    },

    // ***************************************************************************************碰杠胡的处理
    // 碰杠的设置
    // dataArr                      碰杠牌的牌值
    // pengGangNum                  碰杠的数量
    // routeData                    预制体的路径
    // playerID                     玩家的ID
    // showPai                      牌的图集
    // isPengGang                   是碰还是杠   1 为碰 2 为杠
    SetPengGang : function(dataArr, pengGangNum, routeData, playerID, showPai, isPengGang) {
        this.m_gangPeng[playerID].active = true;
        cc.loader.loadRes(routeData, (err, prefab)=>{
            if (!err) {
                var node = cc.instantiate(prefab);
                var pai1 = node.getChildByName("pai1").getChildByName("card").getComponent(cc.Sprite);
                var pai2 = node.getChildByName("pai2").getChildByName("card").getComponent(cc.Sprite);
                var pai3 = node.getChildByName("pai3").getChildByName("card").getComponent(cc.Sprite);

                var frame = showPai.getSpriteFrame(MaJiang.cardPng(dataArr[0]));   
                pai1.spriteFrame = frame;
                pai2.spriteFrame = frame;
                pai3.spriteFrame = frame;
                if(isPengGang == 1)
                {
                    node.getChildByName("pai4").active = false;
                } else if (isPengGang == 2) {
                    var pai4 = node.getChildByName("pai4").getChildByName("card").getComponent(cc.Sprite);
                    pai4.spriteFrame = frame;
                }
                
                if (playerID == 1) {
                    this.m_gangPeng[playerID].addChild(node, this.m_gangChengJi1);
                    this.m_gangChengJi1-=1;
                } else {
                    this.m_gangPeng[playerID].addChild(node);
                }

                // 杠牌的位置
                if(playerID == 0) {
                    node.setPosition(cc.p(240*pengGangNum, 0));
                    this.SetPengGangPlayerCards();
                } else if(playerID == 1) {
                    node.setPosition(cc.p(0, 100*pengGangNum));
                    this.SetOtherPlayerCards(1);
                } else if(playerID == 2) {
                    node.setPosition(cc.p(-157*pengGangNum, 0));
                    this.SetOtherPlayerCards(2);
                } else if(playerID == 3) {
                    node.setPosition(cc.p(0, -100*pengGangNum));
                    this.SetOtherPlayerCards(3);
                }
                
                if (isPengGang == 2)
                {
                    // 背面牌堆的最后一张消失
                    this.SetGangPaiDisappearCard();
                }
            }
        });
    },

    // 碰杠的设置
    // dataArr                      碰杠牌的牌值
    // pengGangNum                  碰杠的数量
    // playerID                     玩家的ID
    // isPengGang                   是碰还是杠   1 为碰 2 为杠
    SetPengGangPai : function(dataArr, pengGangNum, playerID, isPengGang) {
        if(playerID == 0) {
            this.SetPengGang(dataArr, pengGangNum, "Prefabs/Game/HuaShui/PlayerPG0", playerID, this.m_ShowPai, isPengGang);
        } else if(playerID == 1) {
            this.SetPengGang(dataArr, pengGangNum, "Prefabs/Game/HuaShui/PlayerPG1", playerID, this.m_ShowPai13, isPengGang);
        } else if(playerID == 2) {
            this.SetPengGang(dataArr, pengGangNum, "Prefabs/Game/HuaShui/PlayerPG2", playerID, this.m_ShowPai02, isPengGang);
        } else if(playerID == 3) {
            this.SetPengGang(dataArr, pengGangNum, "Prefabs/Game/HuaShui/PlayerPG3", playerID, this.m_ShowPai13, isPengGang);
        }
    },

    // 胡牌的牌设置
    // dataArr                      牌值的数组
    // showPai                      牌的图集
    // paiArr                       牌的父节点
    // isHu                         是不是胡牌的玩家
    SetShowPaiData : function(dataArr, routeData, paiArr, isHu) {
        for (var num = 0; num < 14; num++) {
            paiArr[num].active = false;
        }

        for (var num = 0; num < dataArr.length; num++) {
            // 是胡牌的玩家执行
            if(isHu && num == dataArr.length-1) {
                paiArr[13].active = true;
                var pai = paiArr[13].getChildByName("card").getComponent(cc.Sprite);
                var frame = routeData.getSpriteFrame(MaJiang.cardPng(dataArr[num]));   
                pai.spriteFrame = frame;
            } else {
                paiArr[num].active = true;
                var pai = paiArr[num].getChildByName("card").getComponent(cc.Sprite);
                var frame = routeData.getSpriteFrame(MaJiang.cardPng(dataArr[num]));   
                pai.spriteFrame = frame;
            }
        }
    },
    
    // 胡牌的处理
    // PlayerID                     玩家ID
    // isHu                         是不是胡牌的玩家
    SetHuPai : function(palyerID, isHu) {
        // 所有手牌的父节点
        this.m_ShoPai.active = false;
        // 所有摊牌的父节点
        this.m_tanPaiNode.active = true;
        if(palyerID == 0){
            this.SetShowPaiData(Defines.playerData.info, this.m_ShowPai, this.m_playerTanPai0, isHu);
        } else if (palyerID == 1) {
            this.SetShowPaiData(Defines.playerData.info1, this.m_ShowPai13, this.m_playerTanPai1, isHu);
        } else if (palyerID == 2) {
            this.SetShowPaiData(Defines.playerData.info2, this.m_ShowPai02, this.m_playerTanPai2, isHu);
        } else if (palyerID == 3) {
            this.SetShowPaiData(Defines.playerData.info3, this.m_ShowPai13, this.m_playerTanPai3, isHu);
        }
    },

    // ***************************************************************************************聊天的处理
    // 获取快速聊天的内容
    // num                          点击的对应聊天内容下标
    GetQuickChatContent : function(num) {
        var str = '';
        switch(num) {
            case 1: {
                str = '不要吵啦，专心玩游戏吧';
                break;
            } case 2: {
                str = '不要走，决战到天亮';
                break;
            } case 3: {
                str = '大家好，很高兴见到各位';
                break;
            } case 4: {
                str = '各位不好意思，我要离开一会';
                break;
            } case 5: {
                str = '和你合作真是太愉快了';
                break;
            } case 6: {
                str = '快点啊，都等到我花都谢了';
                break;
            } case 7: {
                str = '你的牌打的太好了';
                break;
            } case 8: {
                str = '你是MM还是GG';
                break;
            } case 9: {
                str = '交个朋友吧，能告诉我你的联系方式吗';
                break;
            } case 10: {
                str = '下次咱们再玩吧，我要走了';
                break;
            } case 11: {
                str = '再见了，我会想念大家的';
                break;
            } case 12: {
                str = '怎么又断线了，网络怎么这么差啊';
                break;
            }
        }

        return str;
    },

    // 快速聊天动画播放完的回调, 表情动画播放完的回调
    onQuickChatAndExpression : function(event) {
        
    },

    // 设置聊天
    // PlayerID                     玩家ID
    // num                          是表情还是文字     0 - 表情          1 - 文字              2 - 聊天的文字
    // playNum                      表情的标记(1-27)，快速聊天的标记(1-12)，文字聊天的文字
    ShowChat : function(PlayerID, num, playNum) {
        // 表情动画的播放
        if(num == 0) {
            this.m_playerChatNode[PlayerID].children[num].active = true;

            var animation = this.m_playerChatNode[PlayerID].children[num].getComponent(cc.Animation);
            // 播放指定动画
            animation.play(playNum);
            animation.on('finished', this.onQuickChatAndExpression, this);
        } else if(num == 1) {           // 显示快速聊天
            this.m_playerChatNode[PlayerID].children[num].active = true;

            // 对应标记转换为快速聊天的文字
            var str = this.GetQuickChatContent(playNum);

            // 设置字体的宽度
            this.m_playerChatNode[PlayerID].children[num].children[0].width = 24 + (str.length-1) * 40;
            this.m_playerChatNode[PlayerID].children[num].height = 64;

            // 设置气泡的长度
            this.m_playerChatNode[PlayerID].children[num].width = 24 + this.m_playerChatNode[PlayerID].children[num].children[0].width;
            this.m_playerChatNode[PlayerID].children[num].height = 64;

            // 设置显示的文字
            this.m_playerChatNode[PlayerID].children[num].children[0].getComponent(cc.Label).string = str;

            // 获取动画组件
            var animation = this.m_playerChatNode[PlayerID].children[num].getComponent(cc.Animation);
            // 播放指定动画
            animation.play('ChatShow');
            animation.on('finished', this.onQuickChatAndExpression, this);
        } else if(num == 2) {           // 显示打字聊天
            this.m_playerChatNode[PlayerID].children[num-1].active = true;

            var strLen = playNum.length/15;
            if(strLen < 1){
                // 设置字体的宽度
                this.m_playerChatNode[PlayerID].children[num-1].children[0].width = 24 + playNum.length * 32;
                this.m_playerChatNode[PlayerID].children[num-1].height = 64;
            } else {
                strLen = Math.ceil(strLen);
                // 字体的宽度设置
                this.m_playerChatNode[PlayerID].children[num-1].children[0].width = 400;
                this.m_playerChatNode[PlayerID].children[num-1].height = 24 + strLen * 32;
            }
            // 设置气泡的大小
            this.m_playerChatNode[PlayerID].children[num-1].width = 24 + this.m_playerChatNode[PlayerID].children[num-1].children[0].width;

            // 设置显示的文字
            this.m_playerChatNode[PlayerID].children[num-1].children[0].getComponent(cc.Label).string = playNum;

            // 获取动画组件
            var animation = this.m_playerChatNode[PlayerID].children[num-1].getComponent(cc.Animation);
            // 播放指定动画
            animation.play('ChatShow');
            animation.on('finished', this.onQuickChatAndExpression, this);
        } else {
            cc.log('没有这个类型的聊天');
        }
    },

    // ***********************************************************其他玩家的处理***********************************************************
    // ***************************************************************************************收到发牌的处理
    // 出牌后删除发出去的牌的处理
    // PlayerID                     玩家ID
    SetOtherPlayerCards : function(PlayerID){
        if(PlayerID == 1) {
            for (var num = 0; num < 14; num++) {
                this.m_MJArrNode1[num].active = false;
            }

            for (var num = Defines.playerData.info1.length-1; num >= 1; --num) {
                this.m_MJArrNode1[num].active = true;
            }

        } else if(PlayerID == 2) {
            for (var num = 0; num < 14; num++) {
                this.m_MJArrNode2[num].active = false;
            }

            for (var num = Defines.playerData.info2.length-1; num >= 1; --num) {
                this.m_MJArrNode2[num].active = true;
            }

        } else if(PlayerID == 3) {
            for (var num = 0; num < 14; num++) {
                this.m_MJArrNode3[num].active = false;
            }

            for (var num = Defines.playerData.info3.length-1; num >= 1; --num) {
                this.m_MJArrNode3[num].active = true;
            }
        }
    },

    // 发牌后添加牌
    // PlayerID                     玩家ID
    SetOtherShowPlayerCards : function(PlayerID){
        if(PlayerID == 1) {
            for (var num = 0; num < Defines.playerData.info1.length-1; num++) {
                this.m_MJArrNode1[num].active = true;
            }
        } else if(PlayerID == 2) {
            for (var num = 0; num < Defines.playerData.info2.length-1; num++) {
                this.m_MJArrNode2[num].active = true;
            }
        } else if(PlayerID == 3) {
            for (var num = 0; num < Defines.playerData.info3.length-1; num++) {
                this.m_MJArrNode3[num].active = true;
            }
        }
    },

    // 删除牌
    // PlayerID                 玩家ID
    // mjId                     删除麻将的牌值（碰杠的牌值）
    DeleteCard : function(PlayerID, mjId){
        if(PlayerID == 1){
            for (var num = 0; num < Defines.playerData.info1.length; num++) {

                if(Defines.playerData.info1[num] == mjId) {
                    Defines.playerData.info1.splice(num, 1);
                    break;
                }
            }
        } else if(PlayerID == 2){
            for (var num = 0; num < Defines.playerData.info2.length; num++) {

                if(Defines.playerData.info2[num] == mjId) {
                    Defines.playerData.info2.splice(num, 1);
                    break;
                }
            }
        } else if(PlayerID == 3){
            for (var num = 0; num < Defines.playerData.info3.length; num++) {

                if(Defines.playerData.info3[num] == mjId) {
                    Defines.playerData.info3.splice(num, 1);
                    break;
                }
            }
        }
    },

    // ***********************************************************其他界面处理***********************************************************


    // ***************************************************************************************按钮
    // 出牌
    shoot : function(mjId) {
        if(mjId == null) {
            return;
        }
        // 将牌发给服务器
        // NetControl.send('出牌', "", (data1)=>{
        //     cc.director.loadScene('huashui');
        // });
        cc.log(this.m_myMJArr[mjId]);

        this.m_cardsDianJi.active = true;                                                          /////////////////////////////////改
        // 删除点击的牌，排序
        this.SetListPlayerCards(mjId);                                                          /////////////////////////////////改
        Defines.playerData.Palyer0PaiDui.push(mjId);
        this.SetPlayerPaiDui(0, Defines.playerData.Palyer0PaiDui);                                                          /////////////////////////////////改
    },

    // 点击牌，牌向上移动，点击2次将点击的牌发送出去
    onMJClicked : function(event) {
        //如果是再次点击，则出牌
        if(event.target == this.m_selectedMJ) {
            // 获得绑定到牌上的牌值
            var sprit = event.target.getChildByName("card").getComponent(cc.Sprite);
            this.shoot(sprit.paiData);
            this.m_selectedMJ.y = 0;
            this.m_selectedMJ = null;
            return;
        }

        if(this.m_selectedMJ != null) {
            this.m_selectedMJ.y = 0;
        }

        event.target.y = 15;
        this.m_selectedMJ = event.target;
    },

    // 碰，杠，胡，过牌
    onOptionClicked : function(event) {
        console.log(event.target.pai);
        if(event.target.name == "peng") {
            var pengCard = [];
            for(var num = 0; num < 3; ++num) {                                                         /////////////////////////////////改
                this.SetListPlayerCards(0x01);
                pengCard.push(0x01);
            }
            // dataArr                      碰杠牌的牌值
            // pengGangNum                  碰杠的数量
            // playerID                     玩家的ID
            // isPengGang                   是碰还是杠   1 为碰 2 为杠
            this.SetPengGangPai(pengCard, 0, 0, 1);
        } else if(event.target.name == "gang") {
            var pengCard = [];
            for(var num = 0; num < 4; ++num) {                                                          /////////////////////////////////改
                this.SetListPlayerCards(0x01);
                pengCard.push(0x01);
            }

            Defines.playerData.info.push(0x01);         // 杠后在后面拿一张牌放到自己的手上

            // dataArr                      碰杠牌的牌值
            // pengGangNum                  碰杠的数量
            // playerID                     玩家的ID
            // isPengGang                   是碰还是杠   1 为碰 2 为杠
            this.SetPengGangPai(pengCard, 1, 0, 2);
        } else if(event.target.name == "hu") {
            
        } else if(event.target.name == "guo") {
            
        }
        this.m_gangPengHu.active = false;
    },

    // 结算界面的分享按钮
    onJieSuanBtn : function(event) {
        Defines.uiCreator.createSettlement(1,'a');
        // Defines.uiCreator.createSettlement(2,'a');
        // Defines.uiCreator.createSettlement(3,'a');
        // Defines.uiCreator.createSettlement(4,'a');
    },

    // 聊天界面按钮
    onChatButton : function(event) {
         Defines.uiCreator.createChat(this);
    },

    // 退出界面按钮
    onOutButton : function(event) {
        Defines.uiCreator.createOutGame(function (node) {
            cc.director.loadScene('Home');
        });
    },





























    // 实验发牌按钮
    onaaaaaaaaaaaaaa:function(event){
        var datasss = 0;
        for (var num = 0; num < Defines.playerData.info.length; num++) {

            if(Defines.playerData.info[num] == 0x01) {
                datasss+=1;
            }
        }

        if(datasss > 2) {
            this.m_gangPengHu.active = true;
        }

        // 发牌的处理
        if(Defines.playerData.info.length < 14) {
            Defines.playerData.info.push(0x01);
       
            this.SetShowPlayerCards();
            this.m_cardsDianJi.active = false;

            this.SetDisappearCard();
         }
    },

    // 实验其他玩家发牌
    onbbbbbbbbbbbbb:function(event){
        var num = 3;
        if(num == 1)
        {
            Defines.playerData.info1.push(0x01);
            this.SetOtherShowPlayerCards(1);
        }
        if(num == 2)
        {
            Defines.playerData.info2.push(0x01);
            this.SetOtherShowPlayerCards(2);
        }
        if(num == 3)
        {
            Defines.playerData.info3.push(0x01);
            this.SetOtherShowPlayerCards(3);
        }
    },

    // 实验其他玩家出牌
    onccccccccccccc:function(event){
        // 玩家的牌
        var num = 3;
        if(num == 1)
        {
            Defines.playerData.info1.pop();
            this.SetOtherPlayerCards(1);
        }
        if(num == 2)
        {
            Defines.playerData.info2.pop();
            this.SetOtherPlayerCards(2);
        }
        if(num == 3)
        {
            Defines.playerData.info3.pop();
            this.SetOtherPlayerCards(3);
        }

        // 其他玩家的牌堆
        Defines.playerData.Palyer1PaiDui.pop();
        Defines.playerData.Palyer1PaiDui.pop();
        Defines.playerData.Palyer2PaiDui.pop();
        this.SetPlayerPaiDui(1, Defines.playerData.Palyer1PaiDui); 
        this.SetPlayerPaiDui(2, Defines.playerData.Palyer2PaiDui); 
        this.SetPlayerPaiDui(3, Defines.playerData.Palyer3PaiDui); 
    },

    // 杠碰的按钮
    onddddddddddddddd:function(event){

        if(this.ndsadhsakdnask == 4)
        {
            this.ndsadhsakdnask = 0;// 碰杠的数量
            this.ndsadhsakdnask111+=1;
        }

        this.ndsadhsakdnask+=1;// 玩家ID

        //  // //  /// / 碰 *********** *********************************** *********
        // var pengCard = [];
        // for(var num = 0; num < 3; ++num) {                                                         /////////////////////////////////改
        //     this.DeleteCard(this.ndsadhsakdnask, 0x01);
        //     pengCard.push(0x01);
        // }
        // this.SetPengGangPai(pengCard, this.ndsadhsakdnask111, this.ndsadhsakdnask, 1);


        // //  // //  /// / 杠 *********** *********************************** *********
        if(this.ndsadhsakdnask == 1) {
            Defines.playerData.info1.push(0x01);         // 杠后在后面拿一张牌放到自己的手上
        }
        else if(this.ndsadhsakdnask == 2) {
            Defines.playerData.info2.push(0x01);         // 杠后在后面拿一张牌放到自己的手上
        }
        else if(this.ndsadhsakdnask == 3) {
            Defines.playerData.info3.push(0x01);         // 杠后在后面拿一张牌放到自己的手上
        }
        
        var pengCard = [];
        for(var num = 0; num < 4; ++num) {                                                          /////////////////////////////////改
            this.DeleteCard(this.ndsadhsakdnask, 0x01);
            pengCard.push(0x01);
        }
        
        this.SetPengGangPai(pengCard, this.ndsadhsakdnask111, this.ndsadhsakdnask, 2);
    },

    // 胡牌的按钮
    oneeeeeeeeeeeeee:function(event){
        for (var playerID = 0; playerID < 4; playerID++) {
            if (playerID == 0) {
                this.SetHuPai(playerID, false);
            } else if (playerID == 1) {
                this.SetHuPai(playerID, true);
            } else if (playerID == 2) {
                this.SetHuPai(playerID, false);
            } else if (playerID == 3) {
                this.SetHuPai(playerID, true);
            }
        }
    },

    // // 聊天按钮
    // omfffffffffffffff:function(event){
    //     // 表情
    //     this.ShowChat(0,0,this.ndsadhsak+1);
    //     this.ShowChat(1,0,this.ndsadhsak+1);
    //     this.ShowChat(2,0,this.ndsadhsak+1);
    //     this.ShowChat(3,0,this.ndsadhsak+1);
    //     if(this.ndsadhsak%3 == 1)
    //     {
    //         // 快速聊天
    //         for (var i = 0; i < 4; i++) {
    //             this.ShowChat(i,1,i+1);
    //         }
    //     }
    //     if(this.ndsadhsak%3 == 2)
    //     {
    //         for (var i = 0; i < 4; i++) {
    //             // 文字
    //             this.ShowChat(i,2,'啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊');
    //         }
    //     }
    //     this.ndsadhsak+=1;
    // },
});